import {CHANG_INPUT_VALUE,ADD_TODO_ITEM,DELETE_TODO_ITEM,INIT_LIST_ACTION} from './actionTypes'
import axios from 'axios'

export const getInputChangeAction = (value) => ({
    type:CHANG_INPUT_VALUE,
    value
})

export const getAddItemAction = () => ({
    type:ADD_TODO_ITEM
})

export const getDeleteItemAction = (index) => ({
    type:DELETE_TODO_ITEM,
    index
})

export const initListAction = (data) => ({
    type:INIT_LIST_ACTION,
    data
})

//当使用redux-thunk后,action返回可以是一个函数了
export const getTodoList = () => {
    return(dispatch) => {
        axios.get('data.json').then((res)=>{
            console.log(res.data);
            const data = res.data;
            const action = initListAction(data)
            // 注意
            dispatch(action)
            
        }).catch((error)=>{
            console.log(error)
        })
    }
}